package com.a2020111222.test.dao.impl;

import com.a2020111222.test.dao.userDao;
import com.a2020111222.test.pojo.user;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class userDaoImpl implements userDao {
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public boolean login(String name, String password) {
        if(name.equals("YangSir")&&password.equals("123")){
            return true;
        }else{
            return false;
        }

    }
//    目标类
    @Override
    public int addUser(user user) {
        System.out.println("添加用户");
        String sql= "insert into user values(?,?,?)";
        Object[] obj = new Object[]{
                user.getId(),
                user.getName(),
                user.getPassword()
        };
        int num=this.jdbcTemplate.update(sql,obj);
        return num;
    }

    @Override
    public int delUser(int id) {
        String sql= "delete from user where id=?";
        int num=this.jdbcTemplate.update(sql,id);
        return num;
    }

    @Override
    public int update(user user) {
        System.out.println("更新用户");
        String sql= "update user set name=?,password=? where id=?";
        Object[] obj = new Object[]{
                user.getName(),
                user.getPassword(),
                user.getId()
        };
        int num=this.jdbcTemplate.update(sql,obj);
        return num;
    }

    @Override
    public user selectFindById(int id) {
        System.out.println("查询用户");
        String sql= "select * from user where id = ?";
        RowMapper<user> rowMapper=new BeanPropertyRowMapper<user>(user.class);
        return this.jdbcTemplate.queryForObject(sql,rowMapper,id);
    }

    @Override
    public List<user> select() {
        System.out.println("查询所有用户");
        String sql= "select * from user";
        RowMapper<user> rowMapper=new BeanPropertyRowMapper<user>(user.class);
        return this.jdbcTemplate.query(sql,rowMapper);
    }

    @Override
    public void test() {
        System.out.println("AOP测试");
    }
}
